<script lang="ts" setup>
	import {
		reactive,
		inject,
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import user from '@/api/user.js'
	import common from '@/api/common.js'
	// import list from '@/components/list.nvue'
	import list2 from '@/components/list2.vue'
	let page = 1
	let pageSize = 10
	let status = ref('loading')
	let gather: any = reactive({
		navList: [{id: 0,text:'全部'}],
		list: [],
		obj: {
			0: [],
			1: [],
			2: [],
			3: [],
			4: []
		}
	})
	const apiTry: Function = inject('$try')
	//下拉刷新阈值
	let _triggered = ref(false)
	let triggered = ref(false)
	let swiperCurrent = ref<any>(0)

	const moreStatus = (val) => {
		status.value = val
	}
	const getArticleList = async (cb = () => {}) => {
		status.value = 'loading'
		const [err, res] = await apiTry(user.getArticleListType({
			page: page,
			pageSize: pageSize,
			authorid: uni.getStorageSync('userinfo').userid,
			typeid: gather.navList[swiperCurrent.value].id
		}))
		gather.obj[swiperCurrent.value] = [...gather.obj[swiperCurrent.value], ...res.data]
		cb()
		if(res.total <= pageSize){
			status.value = 'nomore'
		}else{ 
			moreStatus(res.data.length !== 0 ? 'loadmore' : 'nomore')
		}
		if (res.data.length === 0) return false
		page += 1 // 当前页加1
	}
	//获取分类
	const getType = async () => {
		const [err, res] = await apiTry(common.dictionary('common'))
		if(err) return
		gather.navList = [...gather.navList,...res.data.common]
		for(let i in gather.navList){
			gather.obj[i] = []
		}
		getArticleList()
	} 
	const listSwitch = (e: any) => {
		swiperCurrent.value = e.current
		if(!gather.obj[e.current].length) {
			page = 1
			getArticleList()
			
		}
	}
	//中止触发
	const onAbort = () => {
		triggered.value = false
	}
	//刷新触发
	const onRefresh = () => {
		if (_triggered.value) return
		_triggered.value = true
		if (!triggered.value) triggered.value = true
		page = 1
		getArticleList(() => {
			triggered.value = false
			_triggered.value = false
		})
	}
	//复位
	const onRestore = () => {
		triggered.value = false
		_triggered.value = false
	}
	onLoad(() => {
		getType()
	})
</script>

<template>
	<view class="main flex flex-dir-c">
		<view class="head">
			<u-navbar title="微博"></u-navbar>
			<u-tabs name="text" class="tabs" active-color="rgb(252,186,13)" :list="gather.navList" :is-scroll="false"
				v-model="swiperCurrent"></u-tabs>
		</view>
		<!-- #ifdef APP-NVUE -->
		<!-- <list @listSwitch="listSwitch" :navList="gather.navList" :swiperCurrent="swiperCurrent" :list="gather.list"/> -->
		<!-- #endif -->
		<!-- #ifndef APP-NVUE -->
		<list2 @scrolltolower="getArticleList(()=>{})" :status="status" @listSwitch="listSwitch" :navList="gather.navList" :swiperCurrent="swiperCurrent" :list="gather.obj[swiperCurrent]"
			@onAbort="onAbort" @onRefresh="onRefresh" @onRestore="onRestore"
			:triggered="triggered"
		/>
		<!-- #endif -->
	</view>
</template>

<style src="./index.scss"></style>
